package com.xiaochen.baidu.service.node;

import com.xiaochen.baidu.common.design.strategy.AbstractMultiThreadStrategyRouter;
import com.xiaochen.baidu.common.design.strategy.StrategyHandler;
import com.xiaochen.baidu.model.entity.RequestEntity;
import com.xiaochen.baidu.service.IRedisService;
import com.xiaochen.baidu.service.factory.DefaultStrategyFactory;
import com.xiaochen.baidu.service.thread.CallBackThreadTask;
import com.xiaochen.baidu.service.thread.ClassThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author xc
 * @date 2025/10/12/12:18
 * @desc
 */
@Slf4j
@Service
public class ParseTokenNode extends AbstractMultiThreadStrategyRouter<RequestEntity, DefaultStrategyFactory.StrategyDynamicContext, String> {

    @Resource
    private ParseNode parseNode;
    
    @Override
    protected void multiThread(RequestEntity requestParameter, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        CallBackThreadTask callBackThreadTask = new CallBackThreadTask(strategyDynamicContext.getCode(), requestParameter.getState(), moiuParseService);
        CompletableFuture<String> callBackFuture = CompletableFuture.supplyAsync(callBackThreadTask, threadPoolExecutor);
        String callBackResult = callBackFuture.join();
        log.info("获取kdownSession:{}", callBackResult);
        strategyDynamicContext.setKdownSession(callBackResult);
        //缓存存储kdownSession
        redisService.setValue("baidu:kdownSession", callBackResult, 10 * 60 * 60 * 1000);
    }

    @Override
    protected String apply(RequestEntity requestParameter, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        return route(requestParameter, strategyDynamicContext);
    }

    @Override
    public StrategyHandler<RequestEntity, DefaultStrategyFactory.StrategyDynamicContext, String> map(RequestEntity requestEntity, DefaultStrategyFactory.StrategyDynamicContext strategyDynamicContext) throws Exception {
        return parseNode;
    }
}
